package com.ruoyi.knowledge.video.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.domain.knowledge.Video;
import com.ruoyi.knowledge.video.mapper.VideoMapper;
import com.ruoyi.knowledge.video.service.IVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 视频管理Service业务层处理
 * 
 * @author Jm
 * @date 2025-08-12
 */
@Service
public class VideoServiceImpl implements IVideoService
{

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private RedisCache redisCache;


    // 阅读量业务
    @Override
    public void increaseReadCount(Long videoId) {
        videoMapper.updateReadCount(videoId);
    }

    // ================== 实现接口：Redis键生成方法（改为public） ==================
    @Override
    public String likeUserKey(Long videoId) {
        return "video:like:user:" + videoId;
    }

    @Override
    public String likeCountKey(Long videoId) {
        return "video:like:" + videoId;
    }

    @Override
    public String collectUserKey(Long videoId) {
        return "video:collect:user:" + videoId;
    }

    @Override
    public String collectCountKey(Long videoId) {
        return "video:collect:" + videoId;
    }



    // ================== 实现接口：点赞/收藏业务方法（改为public） ==================
    @Override
    public void likeArticle(Long videoId, Long userId) {
        // 防止重复点赞
        Boolean isMember = redisCache.redisTemplate.opsForSet().isMember(likeUserKey(videoId), userId);
        if (Boolean.TRUE.equals(isMember)) {
            throw new RuntimeException("你已经点赞过了");
        }
        redisCache.redisTemplate.opsForSet().add(likeUserKey(videoId), userId);
        redisCache.redisTemplate.opsForValue().increment(likeCountKey(videoId));
    }

    @Override
    public void collectArticle(Long videoId, Long userId) {
        Boolean isMember = redisCache.redisTemplate.opsForSet().isMember(collectUserKey(videoId), userId);
        if (Boolean.TRUE.equals(isMember)) {
            throw new RuntimeException("你已经收藏过了");
        }
        redisCache.redisTemplate.opsForSet().add(collectUserKey(videoId), userId);
        redisCache.redisTemplate.opsForValue().increment(collectCountKey(videoId));
    }


    /**
     * 查询视频管理
     * 
     * @param id 视频管理主键
     * @return 视频管理
     */
    @Override
    public Video selectVideoById(Long id)
    {
        return videoMapper.selectVideoById(id);
    }

    /**
     * 查询视频管理列表
     * 
     * @param video 视频管理
     * @return 视频管理
     */
    @Override
    public List<Video> selectVideoList(Video video)
    {
        if (video.getDoctor() != null) {
            Long doctorId = video.getDoctor().getId();
            // 存表的时候只存 doctor_id
            video.setDoctorId(doctorId); // ✅ 这行必须加
        }
        if (video.getTopic() != null) {
            Long topicId = video.getTopic().getId();
            video.setTopicId(topicId); // ✅
        }
        return videoMapper.selectVideoList(video);
    }

    /**
     * 新增视频管理
     * 
     * @param video 视频管理
     * @return 结果
     */
    @Override
    public int insertVideo(Video video)
    {
        return videoMapper.insertVideo(video);
    }

    /**
     * 修改视频管理
     * 
     * @param video 视频管理
     * @return 结果
     */
    @Override
    public int updateVideo(Video video)
    {
        return videoMapper.updateVideo(video);
    }

    /**
     * 批量删除视频管理
     * 
     * @param ids 需要删除的视频管理主键
     * @return 结果
     */
    @Override
    public int deleteVideoByIds(Long[] ids)
    {
        return videoMapper.deleteVideoByIds(ids);
    }

    /**
     * 删除视频管理信息
     * 
     * @param id 视频管理主键
     * @return 结果
     */
    @Override
    public int deleteVideoById(Long id)
    {
        return videoMapper.deleteVideoById(id);
    }
}